import com.alibaba.fastjson.JSONObject;
import com.aliyun.opensearch.DocumentClient;
import com.aliyun.opensearch.OpenSearchClient;
import com.aliyun.opensearch.SearcherClient;
import com.aliyun.opensearch.sdk.dependencies.com.google.common.collect.Lists;
import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchClientException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchResult;
import com.aliyun.opensearch.sdk.generated.search.*;
import com.aliyun.opensearch.sdk.generated.search.general.SearchResult;
import pojo.BaseTimuDetail;
import pojo.Data;

import javax.imageio.stream.IIOByteBuffer;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ydw on 2017/7/17.
 */
public class opensearch {


    public static void main(String[] args) throws Exception {
        String appName = "jslx_test";
        String accesskey = "LTAI5djljr6H3GI5";
        String secret = "ZpM73FexeX0bXr91naOSzITtkZJcWf";
        String host = "http://opensearch-cn-hangzhou.aliyuncs.com";

        //创建并构造OpenSearch对象
        OpenSearch openSearch = new OpenSearch(accesskey, secret, host);
//创建OpenSearchClient对象，并以OpenSearch对象作为构造参数
        OpenSearchClient serviceClient = new OpenSearchClient(openSearch);
        testSearch(serviceClient,appName);
//        addDocument(serviceClient,appName);

//


    }

    public static BaseTimuDetail getDetail(){
        BaseTimuDetail baseTimuDetail = new BaseTimuDetail();
        baseTimuDetail.setId_1(2L);
        baseTimuDetail.setTimu_id(3L);
        baseTimuDetail.setAnalysis("2222");
        baseTimuDetail.setAnswer("中国");
        baseTimuDetail.setComment("22222");
        baseTimuDetail.setTrunk("中国人");
        return baseTimuDetail;
    }

    /**
     * 添加文档
     * @param serviceClient
     * @param appName
     * @throws OpenSearchClientException
     * @throws OpenSearchException
     */
    public static void addDocument(OpenSearchClient serviceClient,String appName) throws OpenSearchClientException, OpenSearchException {
        DocumentClient documentClient = new DocumentClient(serviceClient);

       String table_name = "base_timu_detail";
        BaseTimuDetail baseTimuDetail = getDetail();
        Data data=new Data();
        data.setCmd("add");
        data.setFields(baseTimuDetail);
        List<Data> list=new ArrayList<Data>();
        list.add(data);
        String data_ = JSONObject.toJSONString(list);
        System.out.println(data_);
//执行推送操作
        OpenSearchResult osr = documentClient.push(data_, appName, table_name);
        String result = osr.getResult();
        System.out.println(result);
    }

    /**
     * 查询文档
     * @param serviceClient
     * @param appName
     * @throws OpenSearchClientException
     * @throws OpenSearchException
     */
    public static void testSearch(OpenSearchClient serviceClient ,String appName) throws OpenSearchClientException, OpenSearchException {
        //创建SearcherClient对象，并以OpenSearchClient对象作为构造参数
        SearcherClient searcherClient = new SearcherClient(serviceClient);
//定义Config对象，用于设定config子句参数，指定应用名，分页，数据返回格式等等
        Config config = new Config(Lists.newArrayList(appName));
        config.setStart(0);
        config.setHits(5);
//设置返回格式为fulljson格式
        config.setSearchFormat(SearchFormat.JSON);
// 创建参数对象
        SearchParams searchParams = new SearchParams(config);
// 指定搜索的关键词，这里要指定在哪个索引上搜索，如果不指定的话默认在使用“default”索引（索引字段名称是您在您的数据结构中的“索引字段列表”中对应字段。），若需多个索引组合查询，需要在setQuery处合并，否则若设置多个setQuery子句，则后面的子句会替换前面子句
        searchParams.setQuery("default:'中国'");
//设置查询过滤条件
        searchParams.setFilter("id>0");
//创建sort对象，并设置二维排序
        Sort sort = new Sort();
//设置id字段降序
        sort.addToSortFields(new SortField("id", Order.DECREASE));
//若id相同则以RANK相关性算分升序
//        sort.addToSortFields(new SortField("default", Order.INCREASE));
//执行查询语句返回数据对象
        SearchResult searchResult = searcherClient.execute(searchParams);
//以字符串返回查询数据
        String result = searchResult.getResult();
        System.out.println(result);
    }
}
